/**************************************************************************************************
 * light library                                                                                  *
 * Copyright © 2012 David Kretzmer                                                                *
 *                                                                                                *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software  *
 * and associated documentation files (the "Software"), to deal in the Software without           *
 * restriction,including without limitation the rights to use, copy, modify, merge, publish,      *
 * distribute,sublicense, and/or sell copies of the Software, and to permit persons to whom the   *
 * Software is furnished to do so, subject to the following conditions:                           *
 *                                                                                                *
 * The above copyright notice and this permission notice shall be included in all copies or       *
 * substantial portions of the Software.                                                          *
 *                                                                                                *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING  *
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND     *
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.        *
 *                                                                                                *
 *************************************************************************************************/

#include "light/light.hpp"

#ifdef LIGHT_PLATFORM_LINUX

#include "light/io/internal/linux_mmfile.hpp"
#include "light/diagnostics/errors.hpp"
#include "light/utility/safe_int_cast.hpp"

#include <sys/mman.h>


namespace light
{
namespace io
{
namespace internal
{
	namespace
	{
		void* create_mapping(int file, uint8 offset, uint8 length, access acc, mapping map)
		{
			assert(acc & access::read || acc & access::write);

			int acc_impl = 0;
			if(acc & access::read) acc_impl |= PROT_READ;
			if(acc & access::write) acc_impl |= PROT_WRITE;

			int map_impl = map == mapping::shared ? MAP_SHARED : MAP_PRIVATE;

			void *addr = mmap64(nullptr, safe_int_cast<size_t>(length), acc_impl, map_impl, file, safe_int_cast<size_t>(offset));
			if(addr == reinterpret_cast<void*>(-1))
				throw runtime_error("cannot map file");

			return addr;
		}
	}


	// Constructor. Opens a file
	mmfile_base::mmfile_base(utf8_unit const *filename, access acc, mapping flags) :
		m_file(filename, acc)
	{
		m_data = create_mapping(m_file.descriptor(), 0, m_file.size(), acc, flags);
	}

	// Destructor. Closes the file
	mmfile_base::~mmfile_base()
	{
		close();
	}

	// Opens a file
	void mmfile_base::open(utf8_unit const *filename, access acc, mapping flags)
	{
		m_file.open(filename, acc);
		m_data = create_mapping(m_file.descriptor(), 0, m_file.size(), acc, flags);
	}

	// Closes the file
	void mmfile_base::close()
	{
		int unmap_result = munmap(m_data, m_file.size());
		assert(unmap_result == 0);

		m_file.close();
	}

	// Gets the size of the file
	uint8 mmfile_base::size() const
	{
		return m_file.size();
	}

	// Gets the data of the file
	void* mmfile_base::data()
	{
		return m_data;
	}

	void const* mmfile_base::data() const
	{
		return m_data;
	}

	void const* mmfile_base::cdata() const
	{
		return m_data;
	}


} // namespace: internal
} // namespace: io
} // namespace: light


#endif // conditional compilation: LIGHT_PLATFORM_LINUX

